<template>
  <div class="base-curriculum">
    <!--    第一个卡片-->
    <el-card>
      <div class="desc-box">
        <div class="title">课程列表</div>
        <div class="desc">您可以对课程列表进行增删改查操作！</div>
      </div>

      <div class="use-box">
        <div>
          <el-button class="add-button" type="primary" @click="openDialogComponent(0)">添加课程</el-button>
        </div>
        <div class="search-box">
          <span class="search-name">当前启用的学期 : {{ currentSemesterName }}</span>
          <div class="search-input">
            <el-select v-model="queryInfo.SubjectID" placeholder="请选择学科">
              <el-option label="-----查询全部-----" :value="0"></el-option>
              <el-option
                v-for="(item,index) in subjectList"
                :key="index"
                :label="item.Text"
                :value="item.Value">
              </el-option>
            </el-select>
          </div>
          <div class="search-input">
            <el-input
              clearable
              @clear="getTableList"
              v-model="queryInfo.ClassName"
              placeholder="请输入课程名称"></el-input>
          </div>
          <div>
            <el-button
              type="primary"
              class="search-button"
              @click="getTableList">查询
            </el-button>
          </div>
        </div>
      </div>
    </el-card>
    <!--    第二个卡片-->
    <el-card class="second-card">
      <el-table
        ref="table"
        border
        max-height="530px"
        v-loading="loading"
        element-loading-text="拼命加载中......"
        :data="tableData"
        :cell-style="{textAlign:'center'}"
        :header-cell-style="{textAlign:'center'}"
        :header-cell-class-name="setHeaderRowClassName">
        <el-table-column type="index" label="序号" fixed></el-table-column>
        <el-table-column prop="SubjectName" label="学科名称" width="200px"></el-table-column>
        <el-table-column prop="ClassName" label="课程名称" width="200px"></el-table-column>
        <el-table-column label="课程价格/元" width="96px">
          <template slot-scope="scope">
            <p>{{ scope.row.Price | rounding }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="Accommodate" label="容纳人数/人" width="96px"></el-table-column>
        <el-table-column prop="Surplus" label="剩余人数/人" width="96px"></el-table-column>
        <el-table-column prop="ClassHour" label="课程时长/学时" width="110px"></el-table-column>
        <el-table-column prop="Describe" label="课程描述" show-overflow-tooltip width="200px"></el-table-column>
        <el-table-column prop="Image" class="base-curriculum-image" label="课程图片" width="200px">
          <template slot-scope="scope">
            <div class="showImage">
              <el-image
                :src="imageUrl+scope.row.Image"
                :preview-src-list="imageUrlList"
                style="max-height: 100px; max-width: 100px"
              ></el-image>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="启停状态" width="110px">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.Status"
              :active-value="1"
              :inactive-value="0"
              @change="statusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="置顶课程" width="110px">
          <template slot-scope="scope">
            <el-button size="mini" @click="topBaseCurriculum(scope.row.ID)">置顶</el-button>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="290px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="openDialogComponent(scope.row.ID)">编辑</el-button>
            <el-button size="mini" type="danger" @click="deleteInfo(scope.row.ID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页-->
      <pagination :child-msg="queryInfo" @callFather="callFather"></pagination>

    </el-card>
    <!--  添加和编辑的对话框-->
    <add-and-edit-dialog
      v-if="isShowDialogComponent"
      :title="title"
      :is-show-dialog-component="isShowDialogComponent"
      @handleClose="handleClose"
      :judge-id="judgeId"
      :subject-list="subjectList"
    ></add-and-edit-dialog>

  </div>
</template>

<script>
//引入添加编辑对话框组件
import AddAndEditDialog from './childComps/AddAndEditDialog'
import CompanyCurriculum from '../../model/companyCurriculum'
import CompanySubject from '../../model/companySubject'
//引入分页的组件
import Pagination from '../../components/base/pagination/Pagination'

export default {
  name: 'BaseCurriculum',
  components: {
    AddAndEditDialog,
    Pagination
  },
  data () {
    return {
      loading: false,
      title: '',
      isShowDialogComponent: false,
      judgeId: 0,
      tableData: [],
      //获取列表需要传入的参数
      queryInfo: {
        SubjectID: 0,
        ClassName: '',
        Page: 1,
        Limit: 10,
        total: 0
      },
      //当前被启用的学期的id
      currentSemesterId: [],
      //当前被启用的学期的名称
      currentSemesterName: [],
      //保存获取到的学科的信息
      subjectList: [],
      statusChangeForm: {
        id: '',
        status: ''
      },
      //保存图片的域名
      imageUrl: '',
      //图片预览功能
      imageUrlList: [],
    }
  },
  created () {
    this.getTableList()
    this.getSubjectId()
  },
  filters: {
    rounding (price) {
      return price.toFixed(2)
    },
  },
  methods: {
    //分页的回调
    callFather (value) {
      if (value.currentPage) this.queryInfo.Page = value.currentPage
      if (value.pageSize) this.queryInfo.Limit = value.pageSize
      this.getTableList()
    },
    handleClose (payload) {
      if (payload === 'submitButton') {
        this.getTableList()
      }
      this.isShowDialogComponent = false
    },
    //设置绑定学科的表头样式
    setHeaderRowClassName ({ rowIndex }) {
      if (rowIndex === 0) {
        return 'setHeaderSubjectColumnName'
      }
    },
    //表格中的图片大图预览初始化功能
    initImageUrlList () {
      this.tableData.forEach(item => {
        this.imageUrlList.push(this.imageUrl + item.Image)
      })
    },
    //获取课程列表
    async getTableList () {
      try {
        this.loading = true
        const res = await CompanyCurriculum.PageSearch(this.queryInfo)
        this.loading = false
        this.tableData = res.ListData
        this.queryInfo.total = res.Total
        this.imageUrl = res.ImageUrl
        //大图预览的初始化功能
        this.initImageUrlList()
      } catch (err) {
        this.loading = false
      }
    },
    //打开添加、编辑话框
    openDialogComponent (id) {
      this.title = id === 0 ? '添加课程' : '编辑课程'
      this.judgeId = id === 0 ? 0 : id
      this.isShowDialogComponent = true
    },
    //获取学科
    getSubjectId () {
      this.$store.dispatch('getCurrentSemester').then(async result => {
        this.currentSemesterId = result[0].Value
        this.currentSemesterName = result[0].Text
        const res = await CompanySubject.PageSearchList({ ID: this.currentSemesterId })
        console.log(res)
        this.subjectList = res
      })
    },
    //删除操作
    async deleteInfo (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该课程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除该课程的操作')
      } else {
        await CompanyCurriculum.Delete({ ID: id })
        this.$message.success('成功删除该课程')
        await this.getTableList()
      }
    },
    //修改启停状态
    async statusChange (rowInfo) {
      this.statusChangeForm.id = rowInfo.ID
      this.statusChangeForm.status = rowInfo.Status
      await CompanyCurriculum.UpdateStatus(this.statusChangeForm)
      this.$message.success('成功修改课程的启停状态')
    },
    //置顶课程的操作
    async topBaseCurriculum (id) {
      await CompanyCurriculum.Top({ ID: id })
      this.$message.success('置顶课程成功')
      await this.getTableList()
    },
  }

}
</script>

<style scoped>
@import "../../../src/assets/style/realize/tableStyle.css";

.base-curriculum {
  background-color: #eee;
}

.el-card {
  box-shadow: none;
  margin-top: 10px;
}

.second-card {
  padding-bottom: 80px;
  position: relative;
}

.el-pagination {
  position: absolute;
  right: 20px;
  bottom: 20px;
}

.search-name {
  padding: 0 30px;
  color: #DC143C;
  font-size: 15px;
  font-weight: bold;
  line-height: 40px;
}

/*给表头一行加背景颜色*/
/deep/ .setHeaderSubjectColumnName {
  background-color: #E6E6FA !important;
  opacity: 0.7 !important;
}

/*描述文本域明显一些的样式，设置高度*/
/deep/ .el-textarea__inner {
  height: 150px;
}
</style>


